package cn.jinjimi.app.main.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.Constants;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.HonorAddEvent;
import cn.jinjimi.app.controller.event.HonorDelEvent;
import cn.jinjimi.app.controller.event.HonorListEvent;
import cn.jinjimi.app.entity.HonorBean;
import cn.jinjimi.app.main.adapter.HonorListAdapter;
import cn.jinjimi.app.main.dialog.ActionListDialog;
import cn.jinjimi.app.main.dialog.CommAlertDialog;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;
import cn.jinjimi.app.util.PhotoUtils;
import cn.jinjimi.app.util.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by Roy
 * Date: 15/9/6
 */
public class PersonalHonorActivity extends ToolBarCommonActivity implements
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, XListView.IXListViewListener {

    private XListView mListView;
    private HonorListAdapter mAdapter;
    private Button mAddBtn;

    int curPosition;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_honor_list_layout);
        setRightText(R.string.add);
        setTitle(getString(R.string.info_honor));
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        mListView = (XListView) findViewById(R.id.listview);
        mListView.setPullLoadEnable(false);
        mListView.clearLoadAnimate(true);
        mListView.setXListViewListener(this);
        mListView.setTransparentBg();

        View listBottomView = View.inflate(this, R.layout.honor_list_bottom_view, null);
        mListView.addFooterView(listBottomView);
        mAddBtn = (Button) listBottomView.findViewById(R.id.btn_add);
    }

    protected void initListener() {
        mAddBtn.setOnClickListener(this);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
    }

    protected void initData() {
        mAdapter = new HonorListAdapter(PersonalHonorActivity.this);
        mListView.setAdapter(mAdapter);
        onRefresh();
    }

    /**
     * 请求删除荣誉
     *
     * @param id
     */
    private void delHonor(String id) {
        ToastUtil.showLoading(this, getString(R.string.deleting_wait));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHonorListEvent(HonorListEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                List<HonorBean> honorBeans = event.data.getUser_honors();
                mAdapter.setItems(honorBeans);
                updateUI();
                break;
            case DataEvent.FAIL:
                break;
        }
        mListView.stopRefresh();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHonorAddEvent(HonorAddEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                mAdapter.addItemInHead(event.data.getUser_honor());
                updateUI();
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onHonorDelEvent(HonorDelEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                mAdapter.remove(mAdapter.getItem(curPosition));
                updateUI();
                break;
            case DataEvent.FAIL:
                break;
        }
        ToastUtil.hideLoading();
    }

    /**
     * 添加或者删除之后刷新View以及更新AccountManager的值
     **/
    private void updateUI() {
        setResult(RESULT_OK);
        if (mAdapter.getCount() > 0) {
            mAddBtn.setVisibility(View.GONE);
        } else {
            mAddBtn.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_add:
                PhotoUtils.pickPhoto(PersonalHonorActivity.this, true, Constants.HONOR_ASPECT_RATIO);
                break;
        }
    }

    @Override
    protected void rightAction() {
        super.rightAction();
        if (mAdapter.getCount() < Constants.HONOR_MAX_NUM) {
            PhotoUtils.pickPhoto(PersonalHonorActivity.this, true, Constants.HONOR_ASPECT_RATIO);
        } else {
            ToastUtils.toastMessage(this, getString(R.string.honor_limit));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = PhotoUtils.handlePhotoResult(this, data, requestCode, resultCode);
        if (path != null) {
            Intent intent = new Intent(this, PersonalHonorPostActivity.class);
            intent.putExtra(PersonalHonorPostActivity.IMAGE_PATH, path);
            startActivity(intent);
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        curPosition = position - mListView.getHeaderViewsCount();
        final ActionListDialog actionListDialog = new ActionListDialog(this, R.style.TransparentDialog);
        actionListDialog.addTitle(getString(R.string.menu)).addItem(getString(R.string.honor_del), new ActionListDialog.OnClickListener() {
            @Override
            public void didClick(ActionListDialog dialog, String itemTitle) {
                actionListDialog.dismissDialog();
                showDelDialog();
            }
        });
        actionListDialog.showDialog();

        return true;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HonorBean honorBean = mAdapter.getItem(position - mListView.getHeaderViewsCount());
        if (null != honorBean) {
            Intent intent = new Intent(this, PhotoBrowserSingleActivity.class);
            intent.putExtra(PhotoBrowserSingleActivity.URL, honorBean.getImage_url());
            intent.putExtra(PhotoBrowserSingleActivity.TITLE, honorBean.getName());
            startActivity(intent);
        }
    }

    /**
     * 显示删除comfirmDialog
     **/
    private void showDelDialog() {
        final HonorBean honorBean = mAdapter.getItem(curPosition);
        final CommAlertDialog alertDialog = getAlertDialog();
        alertDialog.setTitle(getString(R.string.honor_sure_del))
                .setRightBtn(getString(R.string.ok), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        delHonor(honorBean.getId());
                        alertDialog.dismissDialog();
                    }
                });
        alertDialog.showDialog(CommAlertDialog.Style.TWO_BUTTON);
    }

    @Override
    public void onRefresh() {
        mListView.setRefreshTime(System.currentTimeMillis());
    }

    @Override
    public void onLoadMore() {

    }
}
